
package com.robot.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/**
 * 主菜单窗口
 */

public class MainFrame extends BaseFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 815705495599703503L;
	private JTextPane textPane;
	private JPanel stackPanel ;
	private boolean isRoll = true;
	private JTextPane sendText;


	public MainFrame() {
		this.setSize(640, 520);
		this.setLocationRelativeTo(null);
		this.setContentPane(createContentPane());
	}

	// 创建主pane
	private JPanel createContentPane() {
		JPanel panel = new JPanel(new BorderLayout());
		// 创建一张图片 图表
		panel.add(createMenuPane(), BorderLayout.CENTER);
		return panel;
	}
	
	// 创建菜单项的panel
	private JPanel createMenuPane() {
		JPanel panel = new JPanel(new BorderLayout());
		// 将来这个label 是会变的！！！！！！！！！！！！！！！！！！！！！！
		panel.add(createTextpanel(), BorderLayout.CENTER);
		panel.add(createSendPanel(), BorderLayout.SOUTH);
		return panel;
	}
	private Component createTextpanel() {
		JPanel panel = new JPanel(new BorderLayout());
		textPane = new TextPaneMenu();
		JScrollPane scrollPane = new JScrollPane(textPane);
		panel.add(scrollPane, BorderLayout.CENTER);		
		return panel;
	}
	
	
	public void updateStackMessage(Map<Integer, String> sendMap) {
		for(int i=1;i<=5;i++){
			String text = sendMap.get(i)==null?"(空)":sendMap.get(i);
			((JLabel)stackPanel.getComponent(i-1)).setText(text);
		}
	}

	private SimpleAttributeSet as = new SimpleAttributeSet();
	public void showMsg(String msg, Color color) {
		StyleConstants.setForeground(as, color);
		StyleConstants.setFontSize(as, 12);
		Document doc = textPane.getDocument();
		try {
			doc.insertString(doc.getLength(), msg + "\n", as);
			if (doc.getLength() > 20000)
				doc.remove(0, 2000);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		if (isRoll)
			textPane.setCaretPosition(doc.getLength());
	}
	/**
	 * 发消息
	 */
	private Component createSendPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		sendText = new JTextPane();
		JScrollPane scrollPane = new JScrollPane(sendText);
		sendText.setBackground(new Color(200,255,255));
		JButton sendButton = null;
		try {
			sendButton = new JButton(
					new ImageIcon(ImageIO.read(getClass().getResourceAsStream("images/send.png"))));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendText();
			}

		});
		sendText.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {			
			}
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					sendText();
					sendText.setCaretPosition(sendText.getText().length());
				}	
			}
			public void keyPressed(KeyEvent e) {								
			}
		});
		panel.add(createCheckBox(),BorderLayout.WEST);
		panel.add(scrollPane, BorderLayout.CENTER);
		panel.add(sendButton, BorderLayout.EAST);
		return panel;
	}
	private void sendText() {
		if(sendText.getText().length()>0){
			getContext().publishNotice(System.currentTimeMillis(),sendText.getText());			
		}
		sendText.setText("");
	}
	
	


	private Component createCheckBox() {
		JPanel panel = new JPanel(new BorderLayout());
		final JCheckBox roll = new JCheckBox("滚屏");	
		roll.setSelected(true);
		roll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (roll.isSelected())
					isRoll = true;
				else
					isRoll = false;
			}
		});
		panel.add(roll,BorderLayout.SOUTH);
		return panel;
	}

	public JTextPane getTextPane() {
		return textPane;
	}
	public JTextPane getSendText() {
		return sendText;
	}
	@Override
	public void exit() {
		if(JOptionPane.showConfirmDialog(this, "确定要退出吗？")==JOptionPane.YES_OPTION){	
			getContext().exit();
		}
	}

	
}
